<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

// use %link% to copy the current link and then arguments like page=1 to override the current. End with %

//$txt=strip_tags($site_content);//'blalsdkfjfsk %link%page=3 dsflkjsdlkfj';
$txt=$site_content;

function build_links($txt){
/*
    print_r($txt);
*/
    if ($c=preg_match_all ("/(%link%)(\S[^>\"']+)(%)/", $txt, $matches))
    {
        //print_r($matches);
        //print "($c1) ($word1) ($c2) ($word2) ($int1) \n";
      //echo construct_link(array($arg))."$arg=$value";
        $n = count($matches[0]);
/*
        print_r($matches);
*/


        for ($cnt=0; $cnt < $n; $cnt++){

            $orig = $matches[0][$cnt];
            $args = $matches[2][$cnt];
            $link_array = array();
            $link_string = "";

            if ($c=preg_match_all ("/([a-z]+)(\W)(\d+)/", $args, $matches2)) {
                //print_r($matches2);
                foreach($matches2[0] as $e){
                    //echo $e;
                    $data = explode("=", $e);
                    $arg = $data[0];
                    $value = $data[1];
                    $link_array[] = $arg;
                    $link_string .= "&$arg=$value";
                }
            }
            $link = rtrim(construct_link($link_array), "&").$link_string;
            $txt = str_replace($orig, $link, $txt);

        }

    }
    else {
        //echo "No";
    }
    return $txt;
}

function execute_link_builder($site_content){
    return build_links($site_content);
}

?>
